<template>
  <view class="cart-container">
    <!-- 侧边栏 -->
    <filter-aside></filter-aside>
    <!-- 顶部导航 -->
    <nav-bar></nav-bar>
    <!-- 购物车商品列表 -->
    <view class="goods-list">
      <view class="goods-item" v-for="goods in cartList" :key="goods.id">
        <view class="left">
          <view
            :class="[
              'iconfont',
              'icon-huiyishiqueren_huabanfuben',
              goods.isCheck ? 'check' : '',
            ]"
            @click="changeCheck(goods)"
          ></view>
          <image :src="goods.img" mode="" />
        </view>
        <view class="info">
          <view class="ch-name">{{ goods.name }}</view>
          <view class="price">￥{{ goods.selectItem.price }}</view>
          <view class="tips" v-show="goods.selectItem.fittings">
            标配 <text>含{{ goods.selectItem.fittings }}</text>
          </view>
        </view>
        <view class="right">
          <view
            class="iconfont icon-bianji_o"
            @click="showOverlay(goods)"
          ></view>
          <view class="weight">
            {{ goods.selectItem.spec }}({{ goods.selectItem.weight }})
            <text class="num">x{{ goods.num }}</text>
          </view>
        </view>
      </view>
    </view>
    <!-- 空提示 -->
    <u-empty
      v-if="!cartList.length"
      mode="car"
      icon="http://cdn.uviewui.com/uview/empty/car.png"
    >
    </u-empty>
    <!-- 底部 -->
    <view class="footer-control-cont" v-else>
      <view class="footer-control">
        <view class="left">
          <text
            :class="[
              'iconfont',
              'icon-huiyishiqueren_huabanfuben',
              isAllCheck ? 'check' : '',
            ]"
            @click="allCheck"
          ></text
          >全选
          <view>
            总计:<text class="total">￥{{ totalPrice.toFixed(2) }}</text>
          </view>
        </view>
        <view class="btn" @click="toOrder">立即结算</view>
      </view>
    </view>
    <!-- 遮罩层 -->
    <u-overlay :show="isOverlayShow" @click="isOverlayShow = false">
      <view class="warp">
        <view class="rect" @tap.stop>
          <view class="edit-goods-cont">
            <view class="goods-info">
              <image :src="editGoods.img" mode="" />
              <view class="info">
                <view class="ch-name">{{ editGoods.name }}</view>
                <view class="price">￥{{ editGoods.selectItem.price }}</view>
                <view class="tips" v-show="editGoods.selectItem.fittings">
                  标配 <text>含{{ editGoods.selectItem.fittings }}</text>
                </view>
              </view>
            </view>
            <view class="spec-cont">
              <text>规格选择</text>
              <view class="spec" @click="isShowSpec = true">
                {{ editGoods.selectItem.spec }}
                ({{ editGoods.selectItem.weight }})
                {{ editGoods.selectItem.edible }}
                <text class="iconfont icon-xiala"></text>
                <view class="spec-list" v-show="isShowSpec">
                  <view
                    class="item"
                    v-for="item in editGoods.list"
                    :key="item.id"
                    @click.stop="changeSpec(item)"
                  >
                    {{ item.spec }}({{ item.weight }}) {{ item.edible }}
                  </view>
                </view>
              </view>
            </view>
            <view class="num-cont">
              <text>数量选择</text>
              <u-number-box
                integer
                max="50"
                v-model="editGoods.num"
                button-size="26"
                bgColor="#ffe32a"
                @change="changeNum"
              ></u-number-box>
            </view>
          </view>
        </view>
      </view>
    </u-overlay>
  </view>
</template>

<script>
import { mapState, mapMutations, mapGetters } from 'vuex'
import { getStorage } from '../../utils/auth'
export default {
  data() {
    return {
      isOverlayShow: false, // 是否显示遮罩层
      isShowSpec: false, // 是否显示规格选择
      // 当前编辑的商品
      editGoods: {
        selectItem: {},
      },
    }
  },
  computed: {
    ...mapState('cart', ['cartList']),
    // cartList: {
    //   get() {
    //     return this.$store.state.cart.cartList
    //   },
    //   set(value) {
    //     this.cartList = value
    //   },
    // },
    ...mapState('user', ['userInfo']),
    ...mapGetters('cart', ['isAllCheck', 'totalPrice']),
  },
  methods: {
    ...mapMutations('cart', ['changeCheck', 'allCheck']),
    // 显示遮罩层
    showOverlay(goods) {
      this.editGoods = goods
      this.isOverlayShow = true
    },
    // 改变商品规格
    changeSpec(spec) {
      this.isShowSpec = false
      this.$store.commit('cart/changeSpec', { spec, goods: this.editGoods })
    },
    // 改变商品数量
    changeNum({ value }) {
      this.$store.commit('cart/changeNum', {
        num: value,
        goods: this.editGoods,
      })
    },
    // 跳转订单页
    toOrder() {
      const { cartList } = this

      if (!cartList.some(item => item.isCheck === true)) {
        return uni.showToast({
          title: '请先勾选购买的商品',
          duration: 2000,
          icon: 'none',
        })
      }
      uni.navigateTo({
        url: '/pages/order/order',
      })
    },
    // 初始化购物车数据
    initCartList() {
      getStorage('cartList')
        .then(value => {
          this.$store.commit('cart/initCartList', value)
        })
        .catch(err => {
          console.log(err)
        })
    },
  },
  onShow() {
    // 隐藏tabBar
    uni.hideTabBar()
    // 初始化购物车数据
    this.initCartList()

    // 购物车拦截
    if (this.userInfo) {
      return
    }
    uni.showModal({
      title: '温馨提示',
      content: '您需要登录后才能继续操作',
      cancelText: '稍后再说',
      confirmText: '立即登录',
      success: function (res) {
        if (res.confirm) {
          uni.navigateTo({
            url: '/pages/personal/login',
          })
        } else {
          console.log(123)
          uni.switchTab({
            url: '/pages/home/home',
          })
        }
      },
    })
  },
}
</script>

<style lang="scss">
.cart-container {
  .iconfont.check {
    color: $lxins-color-yellow;
  }
  .goods-item {
    padding: 12rpx 20rpx;
    height: 200rpx;
    display: flex;
    justify-content: space-between;
    align-items: center;
    & > view {
      height: 100%;
    }
    .left {
      display: flex;
      align-items: center;
      .iconfont {
        font-size: $lxins-fs-22;
        margin-right: 12rpx;
        color: #ccc;
      }
      image {
        width: 160rpx;
        height: 160rpx;
      }
    }

    .right {
      display: flex;
      flex-direction: column;
      align-items: end;
      margin-top: 20rpx;
      .iconfont {
        width: 60rpx;
        height: 60rpx;
        background-color: #ddd;
        border-radius: 50%;
        text-align: center;
        line-height: 60rpx;
        margin-bottom: 12rpx;
      }
      .weight {
        font-size: $lxins-fs-12;
        color: #999;
        text {
          margin-left: 8rpx;
          color: #333;
        }
      }
    }
  }
  .info {
    margin-left: 20rpx;
    margin-top: 20rpx;
    width: 300rpx;
    font-size: $lxins-fs-14;
    & > view {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .en-name {
      color: #999;
    }
    .price {
      font-size: $lxins-fs-12;
      line-height: 40rpx;
    }
    .tips {
      font-weight: 600;
      font-size: $lxins-fs-12;
      text {
        margin-left: 8rpx;
        font-weight: normal;
        color: #999;
      }
    }
  }
  // 底部
  .footer-control-cont {
    height: 100rpx;
    .footer-control {
      position: fixed;
      bottom: 0;
      left: 0;
      right: 0;
      height: 100rpx;
      display: flex;
      justify-content: space-between;
      border-top: 1px solid #eee;
      background-color: #fff;
      .left {
        display: flex;
        margin-left: 20rpx;
        line-height: 100rpx;
        .iconfont {
          font-size: $lxins-fs-22;
          color: #ccc;
        }
        view {
          margin-left: 20rpx;
        }
      }
      .btn {
        width: 200rpx;
        height: 100%;
        text-align: center;
        line-height: 100rpx;
        background-color: $lxins-color-yellow;
        font-size: $lxins-fs-14;
      }
    }
  }
  // 遮罩层
  .edit-goods-cont {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: calc(100vw - 80rpx);
    padding-top: 20rpx;
    background-color: #fff;
    border-radius: 12rpx;
    // overflow: hidden;
    .goods-info {
      display: flex;
      padding: 0 20rpx;
      image {
        width: 180rpx;
        height: 180rpx;
        margin-right: 20rpx;
      }
    }
    .spec-cont,
    .num-cont {
      display: flex;
      justify-content: space-between;
      height: 80rpx;
      line-height: 80rpx;
      border-bottom: 1px solid #eee;
      font-size: $lxins-fs-14;
      text {
        margin-left: 20rpx;
      }
      .spec {
        position: relative;
        margin-right: 20rpx;
        width: 400rpx;
        text-align: end;
        .spec-list {
          position: absolute;
          top: 60rpx;
          right: 0;
          background-color: #fff;
          z-index: 99;
          .item {
            width: 100%;
            padding: 0 20rpx;
            text-align: center;
            &:hover {
              background-color: #eee;
            }
          }
        }
      }
    }
    .num-cont {
      .u-number-box {
        margin-right: 20rpx;
      }
    }
  }
}
</style>
